﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL
{
   public class DBhelper
    {
        //声明连接字符串
        private readonly static string StrCon = ConfigurationManager.ConnectionStrings["LogisticsModelConnectionString"].ConnectionString;

        /// <summary>
        /// 执行增删改方法，返回受影响的行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static int ExecuteNonQueryProc(string procName, params SqlParameter[] pams)
        {
            //实例化SqlConnection对象
            using (SqlConnection scon = new SqlConnection(StrCon))
            {
                //实例化SqlCommand对象
                using (SqlCommand scom = new SqlCommand(procName, scon))
                {
                    scom.CommandType = CommandType.StoredProcedure;
                    if (pams != null && pams.Length > 0)
                    {
                        scom.Parameters.AddRange(pams);
                    }
                    //打开连接
                    if (scon.State == System.Data.ConnectionState.Closed)
                    {
                        scon.Open();
                    }
                    return scom.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 执行查询，返回SqlDataReader游标
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReaderProc(string procName, params SqlParameter[] pams)
        {
            //实例化SqlConnection对象
            SqlConnection scon = new SqlConnection(StrCon);
            //判断数据库的状态是否关闭
            if (scon.State == ConnectionState.Closed)
            {
                //打开数据库
                scon.Open();
            }
            //实例化SqlCommand对象
            using (SqlCommand scom = new SqlCommand(procName, scon))
            {
                scom.CommandType = CommandType.StoredProcedure;
                if (pams != null && pams.Length > 0)
                {
                    scom.Parameters.AddRange(pams);
                }
                SqlDataReader resder = scom.ExecuteReader(CommandBehavior.CloseConnection);
                return resder;
            }
        }

        #region ExecuteNonQuery
        /// <summary>
        /// 执行增、删、改方法，返回受影响的行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] pams)
        {
            //创建sqlconnection对象
            using (SqlConnection con = new SqlConnection(StrCon))
            {
                //创建sqlcommand
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    if (pams != null && pams.Length > 0)
                    {
                        //AddRange是指多个对象
                        com.Parameters.AddRange(pams);
                    }

                    //打开连接
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    return com.ExecuteNonQuery();
                }
            }
        }
        #endregion

        #region ExecuteScalar
        /// <summary>
        /// 执行查询，返回首行首页
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] pams)
        {
            //创建sqlconnection对象
            using (SqlConnection con = new SqlConnection(StrCon))
            {
                //创建sqlcommand
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    if (pams != null && pams.Length > 0)
                    {
                        //AddRange是指多个对象
                        com.Parameters.AddRange(pams);
                    }

                    //打开连接
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    return com.ExecuteScalar();
                }
            }
        }
        #endregion

        #region ExecuteReader
        /// <summary>
        /// 执行查询，执行SqlDataReader游标
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pams)
        {
            //创建sqlconnection对象
            SqlConnection con = new SqlConnection(StrCon);
            //创建sqlcommand
            using (SqlCommand com = new SqlCommand(sql, con))
            {
                if (pams != null && pams.Length > 0)
                {
                    //AddRange是指多个对象
                    com.Parameters.AddRange(pams);
                }

                //打开连接
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                // 注：CommandBehavior.CloseConnection表示关闭SqlDataReader的同时把连接也关掉
                return com.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
        #endregion

        #region SqlDataAdapter
        /// <summary>
        /// 调用离线式查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static DataTable SqlDataAdapter(string sql, params SqlParameter[] pams)
        {
            using (SqlDataAdapter data = new SqlDataAdapter(sql, StrCon))
            {
                if (pams != null)
                {
                    data.SelectCommand.Parameters.AddRange(pams);
                }
                DataTable tb = new DataTable();
                data.Fill(tb);
                return tb;
            }
        } 
        #endregion
    }
}
